package org.vaadinetherealintegration.client.example4;

import etrl.camera.Camera;
import etrl.core.Geode;
import etrl.core.Viewer;
import etrl.geometry.Drawable;
import etrl.geometry.IndexArray;
import etrl.geometry.PrimitiveKind;
import etrl.geometry.Vertex;
import etrl.geometry.VertexArray;
import etrl.vecmath.Vector2f;
import etrl.vecmath.Vector3f;
import etrl.vecmath.Vector4f;

/**
 * Class to display things on overlay
 * @author harold
 *
 */
public class Hud
{
	/**
	 * Camera at hud root used to make ortho projection
	 */
	private Camera _camera;
	
	/**
	 * Viewer
	 */
	private Viewer _viewer;
	
    String hud = "/vaadinethereal/VAADIN/widgetsets/org.etherealengine.gwtcanvas.MyWidgetSet/hud.png";

	/**
	 * Constructor
	 */
	public Hud(Viewer viewer)
	{
		_viewer = viewer;
		
		// Instantiate camera
		_camera = new Camera();
		
		_camera.addChild(get2DQuad());
	
		
		// Configure the camera to use orthogonal projection
		recomputeProjectionMatrix();
	}
	
	/**
	 * Camera getter
	 * @return hud's camera
	 */
	Camera getCamera()
	{
		return _camera;
	}
	
	/**
	 * Recompute the camera projection matrix according to viewer size
	 */
	private void recomputeProjectionMatrix()
	{
		int viewport[] = _viewer.getViewport().getValues();
		_camera.setProjectionAsOrtho(viewport[0], viewport[2], viewport[1], viewport[3], -1.f, 1.f);
	}
	
	private Geode get2DQuad()
	{
		Drawable d = new Drawable(PrimitiveKind.TRIANGLE_STRIP);
	
		  
		  Vector4f color = new Vector4f(1.0f, 1.0f, 1.0f, 1.0f);
		  Vector3f normal = new Vector3f(0.0f, 0.0f, 1.0f);
		  
		  Vertex v1 = new Vertex();
		  v1._coordinate = new Vector4f(0.f, 0.f, 0.0f, 1.0f);
		  v1._color = color;
		  v1._normal = normal;
		  v1._texCoord = new Vector2f(0.f, 0.f);
		  
		  Vertex v2 = new Vertex();
		  v2._coordinate = new Vector4f(512.f, 0.f,	0.0f, 1.0f);
		  v2._color = color;
		  v2._normal = normal;
		  v2._texCoord = new Vector2f(1.f, 0.f);
		  
		  Vertex v3 = new Vertex();
		  v3._coordinate = new Vector4f(0.f, 256.f, 0.0f, 1.0f);
		  v3._color = color;
		  v3._normal = normal;
		  v3._texCoord = new Vector2f(0.f, 1.f);
		  
		  Vertex v4 = new Vertex();
		  v4._coordinate = new Vector4f(512.f, 256.f, 0.0f, 1.0f);
		  v4._color = color;
		  v4._normal = normal;
		  v4._texCoord = new Vector2f(1.f, 1.f);
		  
		  VertexArray vertice = new VertexArray();
		  vertice.push_back(v1);
		  vertice.push_back(v2);
		  vertice.push_back(v3);
		  vertice.push_back(v4);
		    
		  IndexArray indice = new IndexArray();
		  indice.push_back(0);
		  indice.push_back(1);
		  indice.push_back(2);
		  indice.push_back(3);
		  
	      d.setVertexArray(vertice);
	      d.setIndexArray(indice);
	      
	      Geode g = new Geode();
	      g.addDrawable(d);
	      g.addTexture(hud);
	      
	      return g;
	}
}
